/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.creatjobmanage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.modules.creatjobmanage.entity.CreatJobPart;
import com.lczyfz.campusinfo.modules.creatjobmanage.service.CreatJobPartService;
import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.creatjobmanage.entity.CreatPartProject;
import com.lczyfz.campusinfo.modules.creatjobmanage.service.CreatPartProjectService;

import java.util.List;

/**
 * 园区项目Controller
 * @author zms
 * @version 2018-03-26
 */
@Controller
@RequestMapping(value = "${adminPath}/creatjobmanage/creatPartProject")
public class CreatPartProjectController extends BaseController {

	@Autowired
	private CreatPartProjectService creatPartProjectService;

	@Autowired
	private CreatJobPartService creatJobPartService;
	@Autowired
	private SystemService systemService;
	@ModelAttribute
	public CreatPartProject get(@RequestParam(required=false) String id) {
		CreatPartProject entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = creatPartProjectService.get(id);
		}
		if (entity == null){
			entity = new CreatPartProject();
		}
		return entity;
	}
	
	@RequiresPermissions("creatjobmanage:creatPartProject:view")
	@RequestMapping(value = {"list", ""})
	public String list(CreatPartProject creatPartProject, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CreatPartProject> page = creatPartProjectService.findPage(new Page<CreatPartProject>(request, response), creatPartProject);
		List<CreatPartProject>creatPartProjects= creatPartProjectService.findList(creatPartProject);
		model.addAttribute("creatPartProjects", creatPartProjects);
		model.addAttribute("page", page);
		return "modules/creatjobmanage/creatPartProjectList";
	}

	@RequiresPermissions("creatjobmanage:creatPartProject:view")
	@RequestMapping(value = "form")
	public String form(CreatPartProject creatPartProject, Model model) {
		CreatJobPart creatJobPart = new CreatJobPart();
		User campusinfoUser = new User();
		List<User> campusinfoUseres = systemService.findUser(campusinfoUser);
		List<CreatJobPart> creatJobParts = creatJobPartService.findList(creatJobPart);
		model.addAttribute("campusinfoUseres", campusinfoUseres);
		model.addAttribute("creatJobParts", creatJobParts);
		model.addAttribute("creatPartProject", creatPartProject);
		return "modules/creatjobmanage/creatPartProjectForm";
	}

	@RequiresPermissions("creatjobmanage:creatPartProject:edit")
	@RequestMapping(value = "save")
	public String save(CreatPartProject creatPartProject, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, creatPartProject)){
			return form(creatPartProject, model);
		}
		creatPartProjectService.save(creatPartProject);
		addMessage(redirectAttributes, "保存园区项目成功");
		return "redirect:"+Global.getAdminPath()+"/creatjobmanage/creatPartProject/?repage";
	}
	
	@RequiresPermissions("creatjobmanage:creatPartProject:edit")
	@RequestMapping(value = "delete")
	public String delete(CreatPartProject creatPartProject, RedirectAttributes redirectAttributes) {
		creatPartProjectService.delete(creatPartProject);
		addMessage(redirectAttributes, "删除园区项目成功");
		return "redirect:"+Global.getAdminPath()+"/creatjobmanage/creatPartProject/?repage";
	}
	@RequiresPermissions("creatjobmanage:creatPartProject:edit")
	@RequestMapping(value = "status")
	public String status(CreatPartProject creatPartProject, RedirectAttributes redirectAttributes) {
		creatPartProject.setEnrollStatus(CampusinfoOrderStat.DONE.getValue());
		creatPartProjectService.status(creatPartProject);
		addMessage(redirectAttributes, "审核园区项目成功");
		return "redirect:"+Global.getAdminPath()+"/creatjobmanage/creatPartProject/?repage";
	}
}